package tr.edu.metu.is.mega.client.course.lecturenote;

import gwtupload.client.IUploader;
import gwtupload.client.IUploader.OnFinishUploaderHandler;
import gwtupload.client.SingleUploader;
import tr.edu.metu.is.mega.client.core.EditDialog;
import tr.edu.metu.is.mega.client.resource.MegaConstants;
import tr.edu.metu.is.mega.client.widgets.MegaTextField;
import tr.edu.metu.is.mega.common.dto.BaseDTO;
import tr.edu.metu.is.mega.common.dto.CourseDTO;
import tr.edu.metu.is.mega.common.dto.LectureNoteDTO;

import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelFactory;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.widget.form.AdapterField;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.layout.FormData;

/**
 * Edit dialog for {@link LectureNoteDTO}
 */
public class LectureNoteEditDialog extends EditDialog<LectureNoteDTO> {

    private SingleUploader uploader;

    private MegaTextField descriptionField;

    private MegaTextField filePathField;

    private FormBinding binding;

    private BeanModelFactory beanModelFactory;

    private CourseDTO course;

    /**
     * Default constructor.
     */
    public LectureNoteEditDialog(CourseDTO course) {
        super(LectureNoteDTO.class.getName(), course);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void beforeInitForm(BaseDTO baseDTO) {
        this.course = (CourseDTO) baseDTO;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void initForm(FormPanel parent, Object... params) {
        setSize(400, 200);

        beanModelFactory = BeanModelLookup.get().getFactory(LectureNoteDTO.class);

        descriptionField = new MegaTextField(LectureNoteDTO.FIELD_DESCRIPTION);
        descriptionField.setFieldLabel("Description");
        parent.add(descriptionField, new FormData("90%"));

        filePathField = new MegaTextField(LectureNoteDTO.FIELD_FILEPATH);
        filePathField.setFieldLabel("File Name");
        filePathField.setEnabled(false);
        parent.add(filePathField, new FormData("90%"));

        uploader = new SingleUploader();
        uploader.setFileInputPrefix(MegaConstants.COURSE_FILES_PATH + course.getCode().hashCode() + "/");
        uploader.addOnFinishUploadHandler(new OnFinishUploaderHandler() {

            @Override
            public void onFinish(IUploader uploader) {
                filePathField.setValue(uploader.getServerInfo().message);
            }
        });
        AdapterField field = new AdapterField(uploader);
        field.setFieldLabel("File");
        parent.add(field, new FormData("90%"));

        binding = new FormBinding(parent, true);

        binding.bind(beanModelFactory.createModel(new LectureNoteDTO()));

    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected LectureNoteDTO guiToDTO() {
        BeanModel model = (BeanModel) binding.getModel();
        LectureNoteDTO dto = model.getBean();
        dto.setCourse(course);
        dto.setFilePath(filePathField.getValue());
        return dto;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void dtoToGUI(LectureNoteDTO dto) {
        binding.bind(beanModelFactory.createModel(dto));
    }

}
